import { defineStore } from "pinia";
import {
    getHandDisinfectDataApi,
    addHandDisinfectDataApi,
    updateHandDisinfectDataApi,
    delHandDisinfectDataApi,
    getHandDisinfectDataUseApi,
    exportHandDisinfectDataApi,
    exportHandDisinfectUseDataApi
} from '@/Api/HandDisinfect'
import EventBus from "@/utils/EventBus";
import { downloadFileData } from "@/utils/downloadFile";

import { useCommon } from "./Common";
const CommonStore = useCommon()

export const useHandDisinfect = defineStore("handDisinfect", {
    state: (): HandDisinfectState => {
        return {
            loading: false,
            searchForm: {
                disinfectName: '',
                type: '',
                model: '',
                capacity: '',
                department: '',

                current: 1,
                pageNo: 1,
                pageSize: 20,
                total: 0,
                showSizeChanger: true,
                pageSizeOptions: ['20', '30', '50', '100'],
                showTotal(total: number) { return `共${total}条` },
                onChange(pageNo: number, pageSize: number) {
                    this.current = pageNo
                    this.pageNo = pageNo
                    this.pageSize = pageSize
                }
            },
            searchInfoForm: {
                disinfectName: '',
                type: '',
                model: '',
                capacity: '',
                department: '',

                current: 1,
                pageNo: 1,
                pageSize: 20,
                total: 0,
                showSizeChanger: true,
                pageSizeOptions: ['20', '30', '50', '100'],
                showTotal(total: number) { return `共${total}条` },
                onChange(pageNo: number, pageSize: number) {
                    this.current = pageNo
                    this.pageNo = pageNo
                    this.pageSize = pageSize
                }
            },
            addForm: {} as HandDisinfect,
            editForm: {} as HandDisinfect,
            exportForm: {
                TimeArr: [],
                startData: '',
                endData: '',
                department: '',
                illArea: ''
            },
            tableData: [],
            tableInfoData: []
        }
    },
    actions: {
        async getHandDisinfectData() {
            this.loading = true
            if (CommonStore.department != '检验科') {
                this.searchForm.department = CommonStore.department
            }
            try {
                const data = await getHandDisinfectDataApi(this.searchForm) as any
                if (data) {
                    this.tableData = data[0]
                    this.tableData.forEach((item, index) => {
                        item.index = (this.searchForm.pageNo! - 1) * this.searchForm.pageSize! + index + 1
                    })
                    this.searchForm.total = data[1][0].total
                }
            } finally {
                this.loading = false
            }
        },
        async addHandDisinfectData() {
            const data = await addHandDisinfectDataApi(this.addForm) as any
            if (!data) {
                this.getHandDisinfectData()
                EventBus.emit('AddSuccess')
            }
        },
        async editHandDisinfectData() {
            const data = await updateHandDisinfectDataApi(this.editForm) as any
            if (!data) {
                this.getHandDisinfectData()
                EventBus.emit('UpdateSuccess')
            }
        },
        async delHandDisinfectData(name: string) {
            const data = await delHandDisinfectDataApi({ name }) as any
            if (!data) {
                this.getHandDisinfectData()
                EventBus.emit('DeleteSuccess')
            }
        },
        async getHandDisinfectDataUse() {

            if (CommonStore.department != '检验科') {
                this.searchInfoForm.department = CommonStore.department
            }
            const data = await getHandDisinfectDataUseApi(this.searchInfoForm) as any
            if (data) {
                this.tableInfoData = data[0]
                this.tableInfoData.forEach((item, index) => {
                    item.index = (this.searchForm.pageNo! - 1) * this.searchForm.pageSize! + index + 1
                })
            }
        },
        async exportHandDisinfectData() {
            if (this.exportForm.TimeArr.length) {
                this.exportForm.startData = this.exportForm.TimeArr[0]
                this.exportForm.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            }

            this.exportForm.department = CommonStore.department == '检验科' ? '' : CommonStore.department
            this.exportForm.illArea = CommonStore.illArea

            const data = await exportHandDisinfectDataApi(this.exportForm) as any
            if (data) {
                downloadFileData(data, '手消液管理.xlsx')
            }
        },
        async exportHandDisinfectUseDataUse() {
            if (this.exportForm.TimeArr.length) {
                this.exportForm.startData = this.exportForm.TimeArr[0]
                this.exportForm.endData = `${this.exportForm.TimeArr[1]} 23:59:59`
            }
            const data = await exportHandDisinfectUseDataApi(this.exportForm) as any
            if (data) {
                downloadFileData(data, '手消液使用记录.xlsx')
            }
        }
    }
})